

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.MenuCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.caigou51.EventBusClass.UpdateShopCarFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateShopCarRedCount;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.CheckOrderActivity;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.card.ShopCarCard;
import com.example.administrator.caigou51.util.ImageUtil;
import com.example.administrator.caigou51.util.MyMathUtial;
import com.example.administrator.caigou51.util.ToastUtil;

import de.greenrobot.event.EventBus;


public class ShopCarCardView extends CardItemView<ShopCarCard> implements View.OnClickListener{

 	private Context mContext;
    private ImageView imageViewCheck;
    private ImageView imageViewGoods;
    private TextView textViewGoodsName;
    private TextView textViewMoneyIcon;
    private TextView textViewGoodsPrice;
    private TextView textViewGoodsKuCun;
    private TextView textViewMoneyIcon2;
    private TextView textViewGoodsTotal;
    private TextView textViewCount;
    private TextView textViewAdd;
    private TextView textViewCountDesc;
    private EditText editTextCount;
    private LinearLayout linearLayoutReduce;
    private View viewReduce;
    private ShopCarCard card;
    private int lastNumber=0;
    public ShopCarCardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public ShopCarCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public ShopCarCardView(Context context,
                           AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void build(final ShopCarCard card) {
        synchronized(this){
        // TODO Auto-generated method stub
        super.build(card);
        this.card=card;
        imageViewCheck = (ImageView) findViewById(R.id.imageViewCheck);imageViewCheck.setOnClickListener(this);
        imageViewGoods = (ImageView) findViewById(R.id.imageViewGoods);
        textViewGoodsName = (TextView) findViewById(R.id.textViewGoodsName);
        textViewMoneyIcon = (TextView) findViewById(R.id.textViewMoneyIcon);
        textViewGoodsPrice = (TextView) findViewById(R.id.textViewGoodsPrice);
        textViewGoodsKuCun = (TextView) findViewById(R.id.textViewGoodsKuCun);
        textViewMoneyIcon2 = (TextView) findViewById(R.id.textViewMoneyIcon2);
        textViewGoodsTotal = (TextView) findViewById(R.id.textViewGoodsTotal);
        linearLayoutReduce = (LinearLayout) findViewById(R.id.linearLayoutReduce);linearLayoutReduce.setOnClickListener(this);
        viewReduce =  findViewById(R.id.viewReduce);viewReduce.setOnClickListener(this);
        textViewAdd = (TextView) findViewById(R.id.textViewAdd);textViewAdd.setOnClickListener(this);
        textViewCountDesc = (TextView) findViewById(R.id.textViewCountDesc);
        editTextCount =  (EditText) findViewById(R.id.editTextCount);
        card.setEditText(editTextCount);

        if (card.getShopCarBean().getCheck())
        {
            imageViewCheck.setImageResource(R.drawable.icon_yes_green);
        }else
        {
            imageViewCheck.setImageResource(R.drawable.icon_circle_grey);
        }

            App.displayImageHttpOrFile(card.getShopCarBean().getThumb(), imageViewGoods, ImageUtil.OptionsActivity());

        lastNumber=card.getShopCarBean().getNumber();
        editTextCount.setText(card.getShopCarBean().getNumber()+"");
        textViewGoodsName.setText(card.getShopCarBean().getTitle()+"\n"+card.getShopCarBean().getProduct_spec());
        textViewGoodsPrice.setText(card.getShopCarBean().getShip_price());
        textViewGoodsKuCun.setText( "库存: "+card.getShopCarBean().getAmount());
        compluteTotal(card);
            editTextCount.setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    try {
                        String str = editTextCount.getText().toString();
                        int i= Integer.parseInt(str);
                        if (i==0) {
                            i = 1;
                            editTextCount.setText(1+"");
                        }
                        lastNumber=i;
                        card.getShopCarBean().setNumber(i);
                        int kucun=Integer.parseInt(card.getShopCarBean().getAmount());
                        if (i>kucun) {
                            card.getShopCarBean().setNumber(kucun);
                            editTextCount.setText(kucun+"");
                            lastNumber=kucun;
                        }
                        compluteTotal(card);
                        EventBus.getDefault().post(new UpdateShopCarFragment());
                        EventBus.getDefault().post(new UpdateShopCarRedCount());
                    }catch (Exception e)
                    {
                        card.getShopCarBean().setNumber(1);
                        editTextCount.setText(1+"");
                        lastNumber=1;
                        compluteTotal(card);
                    }

                    if (!hasFocus)
                    {
                        InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(editTextCount.getWindowToken(),0);
                    }
                }
            });

     /*   editTextCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               if (lastNumber!=card.getShopCarBean().getNumber())
               try {
                   String str = editTextCount.getText().toString();
                   int i= Integer.parseInt(str);
                   lastNumber=i;
                   card.getShopCarBean().setNumber(i);
                   int kucun=Integer.parseInt(card.getShopCarBean().getAmount());
                   if (i>kucun) {
                       card.getShopCarBean().setNumber(kucun);
                       editTextCount.setText(kucun+"");
                       lastNumber=kucun;
                   }
                   compluteTotal(card);
                   EventBus.getDefault().post(new UpdateShopCarFragment());
                   EventBus.getDefault().post(new UpdateShopCarRedCount());
               }catch (Exception e)
               {
                   card.getShopCarBean().setNumber(1);
                   editTextCount.setText(1+"");
                   lastNumber=1;
                   compluteTotal(card);
               }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });*/

    }
    setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            editTextCount.clearFocus();
            return false;
        }
    });
    }

    private void compluteTotal(ShopCarCard card )
    {
        int count =card.getShopCarBean().getNumber();
        textViewCountDesc.setText("共"+count+"件");
        textViewGoodsTotal.setText(MyMathUtial.saveTwoXiaoShu(count* Float.parseFloat(card.getShopCarBean().getShip_price())));

        if (count==1)
        {
            viewReduce.setBackgroundColor(mContext.getResources().getColor(R.color.color_d4d4d4));
        }else
        {
            viewReduce.setBackgroundColor(mContext.getResources().getColor(R.color.color_78828b));
        }
        int kucun=Integer.parseInt(card.getShopCarBean().getAmount());
        if (kucun>count)
        {
            textViewAdd.setTextColor(mContext.getResources().getColor(R.color.color_78828b));
        }else
        {
            textViewAdd.setTextColor(mContext.getResources().getColor(R.color.color_d4d4d4));
        }

        // editTextCount.setText(editCount);
        // textViewTotalCount.setText(count2);
        // textViewTotal.setText(count* Integer.parseInt(goodsDetailBean.getPrice()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.linearLayoutReduce:

                if (card.getShopCarBean().getNumber()>1)
                {

                    card.getShopCarBean().setNumber(card.getShopCarBean().getNumber()-1);
                    editTextCount.setText(card.getShopCarBean().getNumber()+"");
                    compluteTotal(card);
                    EventBus.getDefault().post(new UpdateShopCarFragment());
                    EventBus.getDefault().post(new UpdateShopCarRedCount());
                }else
                {
                    ToastUtil.showToast("不能再减了");
                }


                break;
            case R.id.textViewAdd:
                int kucun=Integer.parseInt( card.getShopCarBean().getAmount());
                if (kucun> card.getShopCarBean().getNumber())
                {
                    card.getShopCarBean().setNumber(card.getShopCarBean().getNumber()+1);
                    editTextCount.setText(card.getShopCarBean().getNumber()+"");
                    compluteTotal(card);
                    EventBus.getDefault().post(new UpdateShopCarFragment());
                    EventBus.getDefault().post(new UpdateShopCarRedCount());
                }else
                {
                    ToastUtil.showToast("库存不够");
                }

                break;
            case R.id.imageViewCheck:
                if (card.getShopCarBean().getCheck())
                {
                    imageViewCheck.setImageResource(R.drawable.icon_circle_grey);
                    card.getShopCarBean().setCheck(false);
                    EventBus.getDefault().post(new UpdateShopCarFragment());
                }else
                {
                    imageViewCheck.setImageResource(R.drawable.icon_yes_green);
                    card.getShopCarBean().setCheck(true);
                    EventBus.getDefault().post(new UpdateShopCarFragment());
                }
                break;
        }
    }


}
